import os
import re

class ScanerUtils:
    source_files = []

    def find_swift_folders(self, directory, skip_folders): 
        self.source_files = []
        self._find_swift_folders(directory, skip_folders)
        return self.source_files

    
    def scanSwiftFile(file):
        frameworks = set()
        with open(file, 'r') as f:
            content = f.read()
            # 匹配 import class 任意多个字母数字.任意多个字母数字换行结尾
            matches = re.findall(r'import\s+([A-Za-z0-9_]+)', content)
            frameworks.update(matches)
            # 匹配 import class|enum|ptotocol|struct *.* 
            matches = re.findall(r'import (?:class|enum|struct|protocol) ([a-zA-Z0-9_]+)\.[a-zA-Z0-9_]+', content)
            # 转换规则
            frameworks.update(matches)
        # remove unused keys
        unused_keys = set(["class", "enum", "struct", "protocol"])
        frameworks -= unused_keys
        return frameworks

    
    def _find_swift_folders(self, directory, skip_folders):
        for item in os.listdir(directory):
            item_path = os.path.join(directory, item)
            if item.endswith(".swift"):
                self.source_files.append(item_path)
            if os.path.isdir(item_path):
                if item in skip_folders:
                    continue
                else:
                    self._find_swift_folders(item_path, skip_folders)
